package com.offer;


import com.utils.DataUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;

public class Test39 {

    private HashMap<Integer, Integer> map = new HashMap<>();

    public int majorityElement(int[] nums) {
        if (nums.length == 1) {
            return nums[0];
        }

        int length = nums.length;

        float half = length * 1.0f / 2;

        for (int num : nums) {
            if (map.containsKey(num)) {
                int currentNum = map.get(num);
                if (currentNum + 1 > half) {
                    return num;
                }
                map.put(num, currentNum + 1);

            } else {
                map.put(num, 1);
            }
        }

        return 0;
    }

    public static void main(String[] args) {


        Test39 test34 = new Test39();
//        int result = test34.majorityElement(new int[]{1, 2, 3, 2, 2, 2, 5, 4, 2});
        int result = test34.majorityElement(new int[]{7, 7, 7, 8, 8});
        System.out.println(result);
    }

}
